﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Common.ExpressionParsing
{
    public class DefaultExtensionManager : IExtensionManager
    {
        private IDictionary<String, IFunctionVisitorExtension> _functionVisitors = null;
        private Object _lockFunctionVisitors = new Object();
        protected IDictionary<String, IFunctionVisitorExtension> FunctionVisitors
        {
            get
            {
                if (_functionVisitors != null)
                    return _functionVisitors;

                lock (_lockFunctionVisitors)
                {
                    if (_functionVisitors == null)
                        _functionVisitors = new Dictionary<String, IFunctionVisitorExtension>();
                }

                return _functionVisitors;
            }
        }

        #region IExtensionManager Members

        public void AddExtension(IFunctionVisitorExtension extension)
        {
            foreach (String functionIdentifier in extension.Identifiers)
                FunctionVisitors.Add(functionIdentifier.ToLower(), extension);
        }

        public bool IsExtensionExist(string functionIdentifier)
        {
            return FunctionVisitors.ContainsKey(functionIdentifier.ToLower());
        }

        public IFunctionVisitorExtension GetFunctionVisitor(string functionIdentifier)
        {
            return FunctionVisitors[functionIdentifier.ToLower()];
        }

        #endregion
    }
}
